//
//  SXStatusOriginalView.swift
//  sina
//
//  Created by shengxinwei on 16/5/23.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let cellMargin: CGFloat = 8
class SXStatusOriginalView: UIView {

    //
    var bottomConstrain: Constraint?
    
    var statusViewModel: SXStatusViewModel? {
        didSet{
            let url = NSURL(string: (statusViewModel?.status?.user?.profile_image_url) ?? "")
            //给控件赋值
            iconView.sd_setImageWithURL(url)
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.time
//            sourceLabel.text = statusViewModel?.status?.source
            sourceLabel.text = statusViewModel?.soucreText
            contentLabel.text = statusViewModel?.status?.text
            //判断是否是大V
            if statusViewModel?.status?.user?.verified == 1 {
                verifiedView.hidden = false
            } else {
                verifiedView.hidden = true
            }
            //判断用户会员等级
            if statusViewModel?.status?.user?.mbrank > 0 && statusViewModel?.status?.user?.mbrank < 7 {
                vipView.hidden = false
                vipView.image = UIImage(named: "common_icon_membership_level\(statusViewModel?.status?.user?.mbrank ?? 0)")
            } else {
               vipView.hidden = true
            }
            
            
            bottomConstrain?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls  where urls.count > 0 {
                picsView.pictures = statusViewModel?.status?.pic_urls
                self.snp_updateConstraints(closure: { (make) in
                    bottomConstrain =  (make.bottom.equalTo(picsView.snp_bottom).offset(cellMargin)).constraint
                })
                picsView.hidden = false
            }else {
                picsView.pictures = nil
                self.snp_updateConstraints(closure: { (make) in
                    bottomConstrain =  (make.bottom.equalTo(contentLabel.snp_bottom).offset(cellMargin)).constraint
                })

                picsView.hidden = true
            }
            
            
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //backgroundColor = UIColor.purpleColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        addSubview(sepView)
        addSubview(iconView)
        addSubview(verifiedView)
        addSubview(nameLabel)
        addSubview(vipView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picsView)
        //设置正文宽度
        let contentWidth: CGFloat = ScreenWidth - 2*cellMargin
        iconView.layer.cornerRadius = 20
        
        sepView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(cellMargin)
        }
        
        //设置约束
        iconView.snp_makeConstraints { (make) in
            make.top.equalTo(sepView.snp_bottom).offset(cellMargin)
            make.left.equalTo(self).offset(cellMargin)
            make.height.width.equalTo(40)
        }
        verifiedView.snp_makeConstraints { (make) in
            make.bottom.right.equalTo(iconView)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(cellMargin)
        }
        vipView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(cellMargin)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(cellMargin)
            make.top.equalTo(timeLabel)

        }
        contentLabel.snp_makeConstraints { (make) in
            make.width.equalTo(contentWidth)
            make.left.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(cellMargin)
        }
        
        picsView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            make.left.equalTo(contentLabel.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) in
            make.left.top.equalTo(self)
            self.bottomConstrain =  (make.bottom.equalTo(picsView.snp_bottom).offset(cellMargin)).constraint
        }
        
        
    }
    
    
    
    
    //MARK: - 懒加载控件
    //头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //昵称
    private lazy var nameLabel: UILabel = {
       let lab = UILabel()
        lab.text = "我是失眠读物"
        lab.textAlignment = .Center
        //lab.font = UIFont.systemFontOfSize(10.0)
        return lab
    }()
    //vip
    private lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //认证图片
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var timeLabel: UILabel = {
        let lab = UILabel()
        lab.text = "22:22"
        lab.textColor = UIColor.redColor()
        lab.font = UIFont.systemFontOfSize(12.0)
        return lab
    }()

    //评论的来源
    private lazy var sourceLabel: UILabel = {
        let lab = UILabel()
        lab.text = "私炮房"
        lab.font = UIFont.systemFontOfSize(12.0)
        return lab
    }()
    //原创正文
    private lazy var contentLabel: UILabel = {
        let lab = UILabel()
        lab.text = "哈哈哈哈,,前排的椅子,,说什么呢哈哈哈哈,,前排的椅子,,说什么呢哈哈哈哈,,前排的椅子,,说什么呢哈哈哈哈,,前排的椅子,,说什么呢哈哈哈哈,,前排的椅子,,说什么呢哈哈哈哈,,前排的椅子,,说什么呢"
        lab.numberOfLines = 0
        return lab
    }()
    
    private lazy var picsView: SXStatusPictureView = SXStatusPictureView()
    
    //添加分割线
    private lazy var sepView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGrayColor()
        return view
    }()
    
    
    
    
    

}
